package com.tju.ocean_ecology_website.dto;

import com.tju.ocean_ecology_website.entity.VolunteerServiceRecord;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 服务时长记录DTO
 * 用于返回给前端的服务时长记录数据
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ServiceHourRecordDTO {
    private Long id;           // 记录ID
    private String date;       // 服务日期
    private Long projectId;    // 项目ID
    private String projectName;// 项目名称
    private Integer hours;     // 服务时长(小时)
    private String status;     // 状态(verified: 已认证, pending: 待认证)
    private String location;   // 服务地点
    private String description;// 服务描述
    
    /**
     * 从VolunteerServiceRecord实体转换为ServiceHourRecordDTO
     * 
     * @param record 服务时长记录实体
     * @return ServiceHourRecordDTO
     */
    public static ServiceHourRecordDTO fromServiceRecord(VolunteerServiceRecord record) {
        if (record == null) {
            return null;
        }
        
        // 格式化日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = record.getServiceDate() != null ? 
                dateFormat.format(record.getServiceDate()) : "";
        
        // 转换状态
        String statusStr;
        if (record.getStatus() == null) {
            statusStr = "pending";
        } else if (record.getStatus() == 1) {
            statusStr = "verified";
        } else {
            statusStr = "pending";
        }
        
        return ServiceHourRecordDTO.builder()
                .id(record.getId())
                .date(formattedDate)
                .projectId(record.getActivityId())
                .projectName(record.getActivityTitle())
                .hours(record.getServiceHours())
                .status(statusStr)
                .location(record.getServiceLocation())
                .description(record.getDescription())
                .build();
    }
}
